package com.example.springLearning.part9.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Random;

@Slf4j
@Component
@Order(1)
public class MyFirstEventListener implements ApplicationListener<MyEvent> {


    Random random = new Random();



    @Override
    public void onApplicationEvent(MyEvent event) {
        log.info("{} received: {}", this.toString(), event);
        //模拟部分失效
//        try {
//            if (random.nextInt(10) % 2 == 1) {
//                throw new RuntimeException("exception happen on first listener");
//            }
//        } catch (Throwable throwable) {
//            log.info("throw runtimeException");
//            throwable.printStackTrace();
//        }

        if (random.nextInt(10) % 2 == 1) {
            throw new RuntimeException("exception happen on first listener");
        }
    }
}
